package sample;
import java.awt.*;
import javafx.scene.control.TextArea;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import sample.Controller;
public class PRI {
    //优先级priority
    public PRI(LinkedList<PCB> pcblist, TextArea txtprocess){
        int max,max_pri,i;
        while (!pcblist.isEmpty()) {
            max=0;
            max_pri=0;
            for (int j = 0; j <pcblist.size(); j++) {
                if (pcblist.get(j).getPriority() > max_pri&&pcblist.get(j).getStatus()!=-1) {//就绪态
                    max = j;
                    max_pri = pcblist.get(j).getPriority();
                }
            }
            pcblist.get(max).setTime(pcblist.get(max).getTime()-1);//剩余时间-1
            pcblist.get(max).setPriority(pcblist.get(max).getPriority()-1);//优先级-1
            if (pcblist.get(max).getPriority()<0)
                pcblist.get(max).setPriority(0);
            if (pcblist.get(max).getTime()<=0)
                //剩余时间为0则从就绪队列中删除该进程
                pcblist.get(max).setStatus(-1);//置为终止态
            for ( i = 0; i < pcblist.size(); i++) {
                txtprocess.appendText(pcblist.get(i).print_pcb());
            }
            if (pcblist.get(max).getStatus()==-1)
            pcblist.remove(max);
           txtprocess.appendText("---------------------------------------------------------------------------------------\n");


        }

    }

}
